package com.example.demo.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/**.html")
                .excludePathPatterns("/**/js/**")
                .excludePathPatterns("/**/css/**")
                .excludePathPatterns("/**/img/**")
                .excludePathPatterns("/**/favicon.ico")
                .excludePathPatterns("/**/fonts/**")
                .excludePathPatterns("/**/druid/**")
                .excludePathPatterns("/**.js")
                .excludePathPatterns("/**.css")
                .excludePathPatterns("/**/webjars/**")
                .excludePathPatterns("/**/swagger-resources/**")
                .excludePathPatterns("/error");

    }


    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter json = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat
        );

        fastJsonConfig.setDateFormat("YYYY-MM-dd HH:mm:ss");

        json.setFastJsonConfig(fastJsonConfig);

        converters.add(json);
    }
}
